(链表)反转链表Reverse List

逆转链表是简单而又简单的链表问题,其问题的方法之一可以设置三个指针,一个指向当前结点,一个指向前驱结点,一个指向后继指针

代码如下:

class Solution {
public:
   ListNode* ReverseList(ListNode* pHead) {
//      if(pHead==NULL || pHead->next==NULL)
//         return pHead;

        ListNode *cur=pHead;
        ListNode *pre=NULL;
        ListNode *tmp;

        while(cur){
            tmp=cur->next;
            cur->next=pre;
            pre=cur;
            cur=tmp;
        }

        return pre;
      }
};
posted @ 2015-04-27 21:48  AndyJee  阅读(1749)  评论(0编辑  收藏  举报